// token 验证中间件
const jwt = require("jsonwebtoken")
const {
    secret
} = require('../config/key')
const verify_token = (req, res, next) => {
    if (req.headers.authorization == undefined) {
        return res.json({
            status: 103,
            msg: '身份无效，请重新登录！err:103'
        })
    }
    const token = req.headers.authorization.split(' ')[1]
    jwt.verify(token, secret, (err, decoded) => {
        if (err) {
            if (err.name == 'TokenExpiredError') {
                return res.json({
                    status: 102,
                    msg: '登录过期，请重新登录！'
                })
            } else if (err.name == 'JsonWebTokenError') {
                return res.json({
                    status: 102,
                    msg: '身份无效，请重新登录！err:102'
                })
            }
        } else {
            next()
        }
    })
}
module.exports = verify_token